import { Request, Response } from 'express'
import { EContainerServiceName, EServiceScope, IContainerService } from '@/types/container'
import Container from '..'

// 获取容器实例
export const getContainer = () => {
  return Container.getInstance()
}

// 清空请求生命周期内的服务实例
export const clearRequestScope = () => {
  getContainer().clearRequestScope()
}

// 绑定服务到容器
export const bindService = <T>(name: EContainerServiceName, service: IContainerService<T>) => {
  getContainer().register<T>(name, service)
}

// 绑定请求对象到容器
export const bindRequest = (request: Request) => {
  bindService<Request>(EContainerServiceName.REQUEST, {
    instance: request,
    constructor: undefined,
    scope: EServiceScope.REQUEST
  })
}

// 绑定响应对象到容器
export const bindResponse = (response: Response) => {
  bindService<Response>(EContainerServiceName.RESPONSE, {
    instance: response,
    constructor: undefined,
    scope: EServiceScope.REQUEST
  })
}
